/**
 * 文件数据准备
 */
package com.flute.framework.configuration;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import com.flute.framework.business.TimerFileData;
import com.flute.framework.data.AbstractRawdataParser;
import com.flute.framework.data.Property;
import com.flute.framework.data.impl.RawDataFileProvider;
import com.flute.framework.data.mysql.MysqlDataPreparer;
import com.flute.framework.data.preparer.FileDataPreparer;
import com.flute.framework.exception.ConfigurationParseException;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

/**
 * @author Jazz
 * 
 */
public class FileDataPreparerXMLConverter {
	public static TimerFileData getTimerFileDataInfo(InputStream inputStream)
			throws ConfigurationParseException {
		try {
			return (TimerFileData) getXStream().fromXML(inputStream);
		} catch (Exception e) {
			throw new ConfigurationParseException(e);
		}
	}

	private static XStream getXStream() {
		XStream xs = new XStream(new DomDriver());
		xs.alias("timer-file-data", TimerFileData.class);
		xs.aliasAttribute(FileDataPreparer.class, "filePreparerName", "name");
		xs.aliasAttribute(FileDataPreparer.class, "tableName", "table-name");
		xs.aliasAttribute(TimerFileData.class, "updateInterval",
				"update-interval");
		xs.aliasField("file-preparer", TimerFileData.class, "preparer");

		xs.alias("file-preparer", FileDataPreparer.class);
		xs.aliasField("mysql-preparer", FileDataPreparer.class, "preparer");
		xs.alias("mysql-preparer", MysqlDataPreparer.class);

		xs.aliasAttribute(MysqlDataPreparer.class, "failedFolder",
				"failed-folder");

		xs.aliasAttribute(MysqlDataPreparer.class, "backupFolder",
				"backup-folder");

		// lu.qiang add
		xs.alias("data-parser", AbstractRawdataParser.class);
		xs.alias("property", Property.class);

		xs.aliasAttribute(Property.class, "name", "name");
		xs.aliasAttribute(Property.class, "value", "value");

		xs.aliasField("global_path", AbstractRawdataParser.class, "globalPath");

		xs.aliasField("data-provider", MysqlDataPreparer.class, "dataProvider");
		xs.aliasField("data-parser", MysqlDataPreparer.class, "parser");
		xs.alias("data-provider", RawDataFileProvider.class);
		xs.aliasAttribute(RawDataFileProvider.class, "path", "path");

		return xs;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			XStream xs = getXStream();
			System.out
					.println(xs
							.toXML(getTimerFileDataInfo(new FileInputStream(
									new File(
											"E:/localDev/quanneiWorkspace/lily/src/framework/config/preparer/filedata/mysongFileDataPreparer.xml")))));
		} catch (ConfigurationParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
